import * as Cesium from 'cesium'

/*
export function getSunDirectionn(viewer = window.cesiumViewer) {
  const julianDate = viewer.clock.currentTime

  // Calculate sun position in Earth-centered inertial coordinates (ECI)
  const sunPositionEci =
    Cesium.Simon1994PlanetaryPositions.computeSunPositionInEarthInertialFrame(julianDate)

  // Convert to Earth-centered fixed (ECF) coordinates
  const icrfToFixed = Cesium.Transforms.computeIcrfToFixedMatrix(julianDate)
  const sunPositionEcf = Cesium.Matrix3.multiplyByVector(
    icrfToFixed,
    sunPositionEci,
    new Cesium.Cartesian3(),
  )

  // Normalize to get direction
  const sunDirectionEcf = Cesium.Cartesian3.normalize(sunPositionEcf, new Cesium.Cartesian3())

  // If you need the direction from a specific earth position:
  const position = Cesium.Cartesian3.fromDegrees(longitude, latitude, height)
  const sunDirection = Cesium.Cartesian3.normalize(
    Cesium.Cartesian3.subtract(sunPositionEcf, position, new Cesium.Cartesian3()),
    new Cesium.Cartesian3(),
  )
}
  */

const transformMatrix = new Cesium.Matrix3()
const sunPositionCartesian = new Cesium.Cartesian3()
export function getSunLightDirectionn(viewer = window.cesiumViewer) {
  const { currentTime } = viewer.clock
  Cesium.Transforms.computeIcrfToCentralBodyFixedMatrix(currentTime, transformMatrix)
  Cesium.Simon1994PlanetaryPositions.computeSunPositionInEarthInertialFrame(
    currentTime,
    sunPositionCartesian,
  )
  Cesium.Matrix3.multiplyByVector(transformMatrix, sunPositionCartesian, sunPositionCartesian)

  Cesium.Cartesian3.normalize(sunPositionCartesian, sunPositionCartesian)
  return sunPositionCartesian
}

/*
const sunCartographicScratch = new Cartographic();
function setSunAndMoonDirections(uniformState, frameState) {
  Transforms.computeIcrfToCentralBodyFixedMatrix(
    frameState.time,
    transformMatrix,
  );

  let position =
    Simon1994PlanetaryPositions.computeSunPositionInEarthInertialFrame(
      frameState.time,
      uniformState._sunPositionWC,
    );
  Matrix3.multiplyByVector(transformMatrix, position, position);

  Cartesian3.normalize(position, uniformState._sunDirectionWC);

  position = Matrix3.multiplyByVector(
    uniformState.viewRotation3D,
    position,
    uniformState._sunDirectionEC,
  );
  Cartesian3.normalize(position, position);

  position =
    Simon1994PlanetaryPositions.computeMoonPositionInEarthInertialFrame(
      frameState.time,
      uniformState._moonDirectionEC,
    );
  Matrix3.multiplyByVector(transformMatrix, position, position);
  Matrix3.multiplyByVector(uniformState.viewRotation3D, position, position);
  Cartesian3.normalize(position, position);

  const projection = frameState.mapProjection;
  const ellipsoid = projection.ellipsoid;
  const sunCartographic = ellipsoid.cartesianToCartographic(
    uniformState._sunPositionWC,
    sunCartographicScratch,
  );
  projection.project(sunCartographic, uniformState._sunPositionColumbusView);
}
  */
